package com.niit.mycourse.service.impl;

import com.niit.entity.Subscribe;
import com.niit.mycourse.dao.SubscribeDao;
import com.niit.mycourse.service.SubscribeService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * (Subscribe)表服务实现类
 *
 * @author makejava
 * @since 2022-11-27 11:25:00
 */
@Service("subscribeService")
public class SubscribeServiceImpl implements SubscribeService {
    @Resource
    private SubscribeDao subscribeDao;

    /**
     * 通过ID查询单条数据
     *
     * @param scid 主键
     * @return 实例对象
     */
    @Override
    public Subscribe queryById(Integer scid) {
        return this.subscribeDao.queryById(scid);
    }

    /**
     * 分页查询
     *
     * @param subscribe 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Subscribe> queryByPage(Subscribe subscribe, PageRequest pageRequest) {
        long total = this.subscribeDao.count(subscribe);
        return new PageImpl<>(this.subscribeDao.queryAllByLimit(subscribe, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param subscribe 实例对象
     * @return 实例对象
     */
    @Override
    public Subscribe insert(Subscribe subscribe) {
        this.subscribeDao.insert(subscribe);
        return subscribe;
    }

    /**
     * 修改数据
     *
     * @param subscribe 实例对象
     * @return 实例对象
     */
    @Override
    public Subscribe update(Subscribe subscribe) {
        this.subscribeDao.update(subscribe);
        return this.queryById(subscribe.getScid());
    }

    /**
     * 通过主键删除数据
     *
     * @param scid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer scid) {
        return this.subscribeDao.deleteById(scid) > 0;
    }

    @Override
    public Subscribe queryBySid(Integer sid) {
        return this.subscribeDao.queryBySid(sid);
    }
}
